package com.qingluan.server;

import org.json.JSONException;
import org.json.JSONObject;

import com.qingluan.arguments.ARGUMENTS;
import com.qingluan.pyi.MainActivity;
import com.qingluan.server.WebSocketBroadcast.ReceiverListener;

import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;
import android.animation.ArgbEvaluator;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class LongSocketServer  extends Service{
	private WebSocketConnection connection;
	final String wsUrl = ARGUMENTS.WS_GET_MESSAGE;
	private WebSocketBroadcast broadcast;
	private String tag = this.getClass().getName();
	
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		IntentFilter fileter = new IntentFilter();
		fileter.addAction(ARGUMENTS.PUSH_SERVICE);
		broadcast = new WebSocketBroadcast();
		broadcast.setReceiveListern(new ReceiverListener(){

			@Override
			public void onReceive(String info) {
				// TODO Auto-generated method stub
				if (connection.isConnected()){
					connection.sendTextMessage(info);
					Log.d(tag, info);
				}else{
					Log.d(tag, "Connected off " );
				}
			}
			
		});
		registerReceiver(broadcast, fileter);
		
		Log.d("Sta", "START .....");
		if (connection != null){
			startConnection();
			Log.d("Sta", "try to open coonecting .....");
		}else{
			connection =  new WebSocketConnection();
			startConnection();
			Log.d("Sta", "try to open coonecting again .....");
		}
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
		Log.d("STA", "Stop server");
		super.onDestroy();
	}
	private void startConnection(){
		connection = new WebSocketConnection();
		Log.d(tag, wsUrl);
		try{
			connection.connect(wsUrl, new WebSocketHandler(){
				@Override
				public void onOpen() {
					// TODO Auto-generated method stub
					Intent intent  = new Intent();
					intent.setAction(ARGUMENTS.RECEIVE_SERVER);
					intent.putExtra("MessagePush", "ON");
					Log.d("Sta","Web  socket connecting ... \n IP : "+wsUrl);
					sendBroadcast(intent);
					
				}
				
				@Override
				public void onTextMessage(String payload) {

					Log.d("Sta","Get message from  IP : "+wsUrl +"\nMessage : "+payload);
					// TODO Auto-generated method stub
					try {
						JSONObject json = new JSONObject(payload);
						if (json.getString("status").equals("login")){
							Log.d(tag, "Add to Room");
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						Log.d(tag,"Not Json Data");
					}
					
					Intent intent  = new Intent();
					intent.setAction(ARGUMENTS.RECEIVE_SERVER);
					intent.putExtra("MessagePush", payload);
					sendBroadcast(intent);
					
					
				}
				
				@Override
				public void onClose(int code, String reason) {
					// TODO Auto-generated method stub

					Log.d("Sta","Web  socket Stop \n IP : "+wsUrl);
					Intent intent  = new Intent();
					intent.setAction(ARGUMENTS.RECEIVE_SERVER);
					intent.putExtra("MessagePush", "OFF");
					sendBroadcast(intent);
					
				}
			});
		}catch( WebSocketException e ){
			Log.d("WebSocketError",e.toString());
		}
	}
	
}
